<?php 
   //ProcessIrEntry.php
   //This file just enters the I&R entry into the DB.  
   //there needs to be code to view and edit specific i&r instances.
		error_reporting(E_PARSE | E_ERROR);
		session_start();
		include('../includes/db_connect.php'); 
		include('../includes/phpheader.php');
		//sanitize data passed from user
		include('../core_lib/core_functions.php');
   //check to see if this is a consumer i&r entry
   $id = $_POST["id"];
		echo $id;
	 if(!$id) {
				//new record
				$id = 0;  
   }
	 echo $id;
   $userId = sanitize($_SESSION['user_id']);
	 // caller info fields, ONLY HOLD VALUES FOR PUBLIC I&Rs
   $callerFirstName    = sanitize($_POST["callerFirstName"]);
   if(!$callerFirstName){
       $callerFirstName=0;
   }
   $callerLastName     = sanitize($_POST["callerLastName"]);
   if(!$callerLastName){
       $callerLastName=0;
   }
   $callerPhoneNumber  = sanitize($_POST["callerPhoneNumber"]);
   if(!$callerPhoneNumber){
       $callerPhoneNumber=0;
   }
   $callerBirthDate  = sanitize($_POST["callerBirthDate"]);
   if(!$callerBirthDate){
       $callerBirthDate=0;
   }
   $callerAddress      = sanitize($_POST["callerAddress"]);
   if(!$callerAddress){
       $callerAddress=0;
   }
   $callerCity         = sanitize($_POST["callerCity"]);
   if(!$callerCity){
       $callerCity=0;
   }
   $callerState_id        = sanitize($_POST["callerState_id"]);
   if(!$callerState_id){
       $callerState=0;
   }
   $callerZip          = sanitize($_POST["callerZip"]);
   if(!$callerZip){
       $callerZip=0;
   }
   $callerCounty       = sanitize($_POST["callerCounty"]);
   if(!$callerCounty){
       $callerCounty=0;
   }
   $dateMonth 												= sanitize($_POST["dateMonth"]);
   $dateDay 													= sanitize($_POST["dateDay"]);
   $dateYear 													= sanitize($_POST["dateYear"]);
   $date = $dateYear."-".$dateMonth."-".$dateDay;
   $minutes 													= sanitize($_POST["minutes"]);
   $irType 														= sanitize($_POST["irType"]);
   $sent 																= sanitize($_POST["sent"]);
   if(!$sent) $sent=0; else $sent=1;
   $sentInfo 													= sanitize($_POST["sentInfo"]);
   if(!$sentInfo) $sentInfo="";
   $sentAgency 											= sanitize($_POST["sentAgency"]);
   if(!$sentAgency) $sentAgency="";
   $sentMinutes 										= sanitize($_POST["sentMinutes"]);
   if(!$sentMinutes) $sentMinutes=0;
   $refer 															= sanitize($_POST["refer"]);
   if(!$refer) $refer=0; else $refer=1;
   $referInfo 												= sanitize($_POST["referInfo"]);
   $referAgency 										= sanitize($_POST["referAgency"]);
   $referMinutes 										= sanitize($_POST["referMinutes"]);
   $info 																= sanitize($_POST["info"]);
   if(!$info) $info=0; else $info=1;
   $infoInfo 													= sanitize($_POST["infoInfo"]);
   $infoAgency 											= sanitize($_POST["infoAgency"]);
   $infoMinutes 										= sanitize($_POST["infoMinutes"]);
   $outcomeHealthcare 						= sanitize($_POST["outcomeHealthcare"]);
   if(!$outcomeHealthcare) $outcomeHealthcare=0; else $outcomeHealthcare=1;
   $outcomeTransportation 			= sanitize($_POST["outcomeTransportation"]);
   if(!$outcomeTransportation) $outcomeTransportation=0; else $outcomeTransportation=1;
   $outcomeAssistiveTech 			= sanitize($_POST["outcomeAssistiveTech"]);
   if(!$outcomeAssistiveTech) $outcomeAssistiveTech=0; else $outcomeAssistiveTech=1;
   $notes 															= sanitize($_POST["notes"]);
   //New assignment option
   $assignedTo 											= sanitize($_POST["assignmentForUser"]);
   if(!assignedTo){
       $assignedTo = 0;
   }
   $stored_ir_entry 							= sanitize($_POST["stored_ir_entry"]);
	 //handle the data
	 if(!$stored_ir_entry){
			//THERE IS NO EXISTING I&R RECORD, SO INSERT INFO INTO DATABASE
    $stored_ir_entry=0; 
    //CREATES NEW I&R RECORD 
    //look up last I&R instance and get the next available record number
    $query = "Select max(id+1) as id from irEntry";
    $result = mysql_query($query);
    if($row=mysql_fetch_array($result)){
	     $irId = $row["id"];
			} else {
	     $irId = NULL;
    }
    $query = "INSERT INTO `irEntry` ( `id` , 
                                        `consumer_id` ,
                                         `callerFirstName`,
                                         `callerLastName`,
                                         `callerPhoneNumber`,
                                         `callerBirthDate`,
                                         `callerAddress`,
                                         `callerCity`,
                                         `callerState_id`,
                                         `callerZip`,
                                         `callerCounty`,  
                                         `date` , 
                                         `type_id` , 
                                         `minutes` , 
                                         `sent` , 
                                         `sent_info` , 
                                         `sent_agency` , 
                                         `sent_minutes` , 
                                         `refer` , 
                                         `refer_info` , 
                                         `refer_agency` , 
                                         `refer_minutes` , 
                                         `info` , 
                                         `info_info` , 
                                         `info_agency` , 
                                         `info_minutes` , 
                                         `outcome_healthcare` , 
                                         `outcome_transportation` , 
                                         `outcome_assistive_tech` , 
                                         `notes` ,
                                         `assignedTo`, 
                                         `entered_by` )
                                 VALUES ($irId , 
                                         $id ,
                                         '$callerFirstName',
                                         '$callerLastName',
                                         '$callerPhoneNumber',
                                         '$callerBirthDate',
                                         '$callerAddress',
                                         '$callerCity',
                                         '$callerState_id',
                                         '$callerZip',
                                         '$callerCounty', 
                                         '$date', 
                                         '$irType', 
                                         '$minutes', 
                                         '$sent',  
                                         '$sentInfo', 
                                         '$sentAgency', 
                                         '$sentMinutes', 
                                         '$refer', 
                                         '$referInfo', 
                                         '$referAgency', 
                                         '$referMinutes', 
                                         '$info', 
                                         '$infoInfo', 
                                         '$infoAgency', 
                                         '$infoMinutes', 
                                         '$outcomeHealthcare', 
                                         '$outcomeTransportation', 
                                         '$outcomeAssistiveTech', 
                                         '$notes',
                                         '$assignedTo', 
                                         '$userId');";
   }else{ 
          //AN EXISTING RECORD IS FOUND, JUST UPDATE IT!
         $query = "UPDATE irEntry SET  id= '".$stored_ir_entry."', 
                                       consumer_id = '".$id."' ,
                                       callerFirstName = '".$callerFirstName."',
                                       callerLastName = '".$callerLastName."',
                                       callerPhoneNumber = '".$callerPhoneNumber."',
                                       callerBirthDate = '".$callerBirthDate."',
                                       callerAddress = '".$callerAddress."',
                                       callerCity = '".$callerCity."',
                                       callerState_id = '".$callerState_id."',
                                       callerZip = '".$callerZip."',
                                       callerCounty = '".$callerCounty."',
                                       date = '".$date."' , 
                                       type_id = '".$irType."' , 
                                       minutes = '".$minutes."' ,
                                       sent = '".$sent."' ,
                                       sent_agency = '".$sentAgency."' , 
                                       sent_info = '".$sentInfo."', 
                                       sent_minutes = '".$sentMinutes."' , 
                                       refer = '".$refer."' , 
                                       refer_info = '".$referInfo."' , 
                                       refer_agency = '".$referAgency."' ,
                                       refer_minutes = '".$referMinutes."' , 
                                       info = '".$info."' , 
                                       info_info = '".$infoInfo."' , 
                                       info_agency = '".$infoAgency."' , 
                                       info_minutes = '".$infoMinutes."' , 
                                       outcome_healthcare = '".$outcomeHealthcare."' , 
                                       outcome_transportation = '".$outcomeTransporation."' , 
                                       outcome_assistive_tech = '".$outcomeAssistiveTech."' , 
                                       notes = '".$notes."' ,
                                       assignedTo= '".$assignedTo."' , 
                                       entered_by = '".$userId."'
                                   WHERE id= '".$stored_ir_entry."';";
                                   //echo "<h1>$stored_ir_entry</h1>";
                                   //exit();
   } 
		//check your result
   if(mysql_query($query)) {
      echo "Success <br/>".$query;
      if($id > 0) {
						//echo "<a href=\"../admin.php?action=cieedit&id=".$id."&form=monthlyISR&isrId=0\">url</a>";
         //there was a consumer i&r entered. take user to casenote section for consumer file
						header("Location: ../admin.php?action=cieedit&id=".$id."&form=monthlyISR&isrId=0&irId=".$irId);
      } else {
						//this was a public i&r. tell the user there was success
						header("Location:../admin.php?action=cieedit&id=".$id."&Status=Your I-R entry was stored in the HOCTIL consumer database.  Please conduct a new intake for this person if necessary.");
      }  
      //exit();	
  } else {
      //error
      header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process I and R Form due to system error.  Please contact system administrator.");
      echo "Failure <br/>".$query;
				//exit();
  }
?>